﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.ZX.ViewModel
{
	public class BaseViewModel:IBase
	{
		private EnumObjState state = EnumObjState.Initializing;
		public EnumObjState State
		{
			get { return state; }
			set
			{
				if (state == value)
					return;
				EnumObjState oldState = state;
				state = value;
				StateChange(this, state, oldState);
               
			}
		}
		public event StateChangeEvent StateChange;

        public BaseViewModel()
        {
            state = EnumObjState.Initializing;
            onInit();
        }

        public void Load()
		{
			if (state != EnumObjState.Initializing)
				return;
			State = EnumObjState.Loading;

			onLoad();

			State = EnumObjState.Ready;
		}
		public void Release()
		{
			onRelease();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="newState"></param>
		/// <param name="oldState"></param>
		protected virtual void onStateChange(EnumObjState newState,EnumObjState oldState)
		{

		}

		public virtual void onDestroy()
		{
			//throw new NotImplementedException();
		}

        public virtual void onInit()
		{
			//throw new NotImplementedException();
		}

		protected virtual void onLoad()
		{

		}

		protected virtual void onRelease()
		{

		} 

		
	}
}
